<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


/**
 * Class for generating shift report pdf.
 *
 * @category RedCross
 * @package RedCross_Pdf
 */

require_once('Base.php');

class RedCross_Pdf_ShiftReport extends RedCross_Pdf_Base
{
	/**
	 * Constructor
	 */
	public function __construct()
	{
		parent::__construct('Vaktrapport');
	}

	/**
	 * Draws the shift report with the provided details.
	 */
	public function generate(RedCross_Gateway_Base_ShiftReport $shift_report)
	{ 
		$shift = $shift_report->Shift;
		$appearance_date = new RedCross_Date($shift->appearance_time,
					RedCross_Date::DB_FMT);
		$from_date = new RedCross_Date($shift->from_date,
					RedCross_Date::DB_FMT);
		$to_date = new RedCross_Date($shift->to_date,
					RedCross_Date::DB_FMT);

		$dudes = 0;
		$show = array();
		$swap = array();
		$noshow = array();
		$leader = array();
		foreach ($shift->Roles as $shift_role)
		{
			if ($shift_role->leader)
			{
				$leader[] = $shift_role;
			}

			if (!is_null($shift_role->attended_member_id))
			{
				++$dudes;
				if ($shift_role->member_id !== $shift_role->attended_member_id)
				{
					$swap[] = $shift_role;
					$show[] = $shift_role;
				}
				else
				{
					$show[] = $shift_role;
				}
			}
			else
			{
				$noshow[] = $shift_role;
			}
		}
		$duration = (float)($to_date->getTimestamp() - $appearance_date->getTimestamp()) / 3600.0;

		$rep = array(self::make_row(self::make_cell('VAKTRAPPORT', 196, 1, 'B', 'C', 24)),
			self::make_row(self::make_cell('RAPPORT', 196, 1, 'B', 'C')),
			self::make_row(self::make_cell('Dato:', 46, 1),
				self::make_cell($appearance_date->getDateStr(), 52),
				self::make_cell('Oppmøte depot', 46, 1),
				self::make_cell($appearance_date->getTimeStr(), 52)),
			self::make_row(self::make_cell('Arrangement', 46, 1),
				self::make_cell($shift->Event->name, 52),
				self::make_cell('Vakta avsluttet', 46, 1),
				self::make_cell($to_date->getTimeStr())),
			self::make_row(self::make_cell('Vaktleder', 46, 1),
				self::make_cell(count($leader) > 0 ?
					($leader[0]->Member->first_name .
					' ' . $leader[0]->Member->last_name) :
					'Unknown', 52),
				self::make_cell('Timer vakt', 46, 1),
				self::make_cell($duration, 52)),
			self::make_row(self::make_cell('',
					46, 0, '', 'L', 12, 'LT'),
				/* FIXME: who are supposed to be listed here? */
				self::make_cell('', 52, 0, '', 'L',
					12, 'TR'),
				self::make_cell('Totalt ant. mannskap', 46, 1),
				self::make_cell($dudes, 52)),
			self::make_row(self::make_cell('', 46, 1, '', 'L', 12, 'LB'),
				self::make_cell('', 52, 0, '', 'L', 12, 'RB'),
				self::make_cell('Ant. tjenestetimer totalt', 46, 1),
				self::make_cell($dudes * $duration, 52)),
			self::make_row(self::make_cell('MANNSKAP', 196, 1, 'B', 'C')));

		$i = 0;
		$whole = count($show) - count($show) % 2;
		for (; $i < $whole; $i += 2)
		{
			$rep[] = self::make_row(self::make_cell($show[$i]->Role->name, 46, 1),
					self::make_cell($show[$i]->Attendant->first_name . ' ' .
						$show[$i]->Attendant->last_name, 52, 0),
					self::make_cell($show[$i + 1]->Role->name, 46, 1),
					self::make_cell($show[$i + 1]->Attendant->first_name . ' ' .
					$show[$i + 1]->Attendant->last_name, 52, 0));
		}

		for (; $i < count($show); ++$i)
		{
			$rep[] = self::make_row(self::make_cell($show[$i]->Role->name, 46, 1),
					self::make_cell($show[$i]->Attendant->first_name . ' ' .
						$show[$i]->Attendant->last_name, 150, 0));
		}

		$rep[] = self::make_row(self::make_cell('BYTTER', 196, 1, 'B', 'C'));
		$imax = count($swap);
		for ($i = 0; $i < $imax; ++$i)
		{
			if ($i === 0)
			{
				$t1 = 'Vaktbytte';
				$t2 = 'Bytte med';
				$border = 'LTR';
			}
			else if ($i === $imax - 1)
			{
				$t1 = '';
				$t2 = '';
				$border = 'LBR';
			}
			else
			{
				$t1 = '';
				$t2 = '';
				$border = 'LR';
			}

			$from = $swap[$i]->Member;
			$to = $swap[$i]->Attendant;

			$rep[] = self::make_row(self::make_cell($t1, 46,
						1, '', 'L', 12, $border),
					self::make_cell($from->first_name .
						' ' . $from->last_name, 52, 0,
						'', 'L', 12, $border),
					self::make_cell($t2, 46, 1, '',
						'L', 12, $border),
					self::make_cell($to->first_name . ' ' .
						$to->last_name, 52, 0, '', 'L',
						12, $border));
		}

		/* This looks like a yank-paste, but was done since we do not
		 * know what to do with the reason for the no show yet. */
		$imax = count($noshow);
		for ($i = 0; $i < $imax; ++$i)
		{
			if ($i === 0)
			{
				$t1 = 'Ikke møtt';
				$t2 = 'Årsak';
				$border = 'LTR';
			}
			else if ($i === $imax - 1)
			{
				$t1 = '';
				$t2 = '';
				$border = 'LBR';
			}
			else
			{
				$t1 = '';
				$t2 = '';
				$border = 'LR';
			}

			$dude = $noshow[$i]->Member;
			$rep[] = self::make_row(self::make_cell($t1, 46,
						1, '', 'L', 12, $border),
					self::make_cell($dude->first_name .
						' ' . $dude->last_name, 52, 0,
						'', 'L', 12, $border),
					self::make_cell($t2, 46, 1, '',
						'L', 12, $border),
					self::make_cell('', 52, 0, '', 'L',
						12, $border));
		}

		$rep[] = self::make_row(self::make_cell('SKADER', 196, 1,
				'B', 'C'));
		$rep[] = self::make_row(self::make_cell(
				$shift_report->injuries, 196));
		$rep[] = self::make_row(self::make_cell('FORBRUKT MATRIELL',
				196, 1, 'B', 'C'));
		$rep[] = self::make_row(self::make_cell(
				$shift_report->equipment_list, 196));
		$rep[] = self::make_row(self::make_cell('MELDINGER',
				196, 1, 'B', 'C'));
		$rep[] = self::make_row(self::make_cell(
				$shift_report->report, 196));

		foreach ($rep as $row)
		{
			$this->MultiRow($row);
			$this->Ln();
		}
	}
}
